﻿using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Project.Core.IServices;
using Project.Core.IServices.System;
using Project.Core.Model;
using Project.Core.Model.System;

namespace Project.Core.Api.Controllers
{
    /// <summary>
    /// 登录
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    [Authorize(Policy = "Client")]
    [Authorize(Roles = "Admin,Client")]
    public class LoginController : Controller
    {
        /// <summary>
        /// 用户类接口实例
        /// </summary>
        public IUserService _userService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userService"></param>
        public LoginController(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Entity">用户实例</param>
        /// <returns></returns>
        [HttpPost]
        [Route("Token")]
        [AllowAnonymous]//不受授权控制，任何人都可访问
        public JsonResult Login(User Entity)
        {
            var time1 = DateTime.Now;
            var time2 = DateTime.Now.AddHours(12);
            TimeSpan ts = time2 - time1;
            string jwtStr = Token.CoreToken.IssueJWT(Entity, ts, ts);
            return Json(jwtStr);
        }
    }
}